//
//  LazyCartViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/7.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "LazyCartViewController.h"
#import "AFNetworkHandler.h"
#import "MyHeaderView.h"
#import "ShopManagerVCCell.h"
#import "MJChiBaoZiHeader.h"
#import "html5MYViewController.h"
#import "SetViewController.h"
#import "html5MYViewController.h"
#import "WholeSaleSubVC.h"


@interface LazyCartViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    UIView *popView;
    UIImageView *barCodeImg;
}
@property (strong, nonatomic) MyHeaderView *headerView;


@end

@implementation LazyCartViewController

-(UIStatusBarStyle)preferredStatusBarStyle {//修改状态栏的颜色
    return UIStatusBarStyleLightContent;
}
-(void)viewWillAppear:(BOOL)animated {
    _headerView.userName.text = [ShopCommonModel share].shopname; // 懒猫社长三星路
    _headerView.shopUserLable.text = [ShopCommonModel share].username; // 社长：Jikee
    _headerView.yueLable.text = [ShopCommonModel share].cashbalance; // 账户余额
    _headerView.shouruLable.hidden = YES;
    _headerView.leijiLable.hidden = YES;
    _headerView.todayOrder.text = [ShopCommonModel share].todayorderscount; // 今日订单个数
    _headerView.waitSend.text = [ShopCommonModel share].unfilledorderscount; // 代发货
    _headerView.addUserLable.text = [ShopCommonModel share].yesterdaynewuserscount; // 昨日新增用户
     [_headerView.headerImg sd_setImageWithURL:[NSURL URLWithString:[[ShopCommonModel share].shoplogo safeString]] placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackColor;
    [self initNav:@"我的" doBack:YES];
    //创建表视图
    [self createTable];
    //刷新方法
    _tableView.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(addRefresh)];
}
- (void)addRefresh {//刷新方法
//    MJChiBaoZiHeader *header = [MJChiBaoZiHeader headerWithRefreshingBlock:^{
//        [header beginRefreshing];
    [self getData];
    [_tableView.header endRefreshing];
//    }];
//    _tableView.header = header;
//    header.lastUpdatedTimeLabel.hidden = YES;
}
-(void)getData {
    [_tableView.header endRefreshing];
}
-(void)createTable {//创建表视图
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 50) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _headerView = [[[NSBundle mainBundle] loadNibNamed:@"MyHeaderView" owner:self options:nil] firstObject];
    _tableView.tableHeaderView = _headerView;
     [self.view addSubview:_tableView];
    UILabel *addButton = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 180, 140, 77, 30)];
    addButton.textAlignment = NSTextAlignmentCenter;
    addButton.text = @"充值";
    addButton.hidden = YES;//暂时先隐藏掉
    addButton.layer.borderWidth = 2;
    addButton.layer.borderColor = BackColor.CGColor;
    addButton.layer.cornerRadius = 3;
    addButton.textColor = SetColor(0, 170, 238, 1);
    addButton.userInteractionEnabled = YES;
    [_headerView addSubview:addButton];
    UITapGestureRecognizer *addTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addAction)];
    [addButton addGestureRecognizer:addTap];
    
    UILabel *tixianButton = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 90, 232, 77, 30)];
    tixianButton.textAlignment = NSTextAlignmentCenter;
    tixianButton.text = @"提现";
    tixianButton.layer.borderWidth = 2;
    tixianButton.layer.borderColor = BackColor.CGColor;
    tixianButton.layer.cornerRadius = 3;
    tixianButton.textColor = SetColor(0, 170, 238, 1);
    tixianButton.userInteractionEnabled = YES;
    [_headerView addSubview:tixianButton];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tixianAction)];
    [tixianButton addGestureRecognizer:tap];
}
#pragma mark - 充值
-(void)addAction {
    
}
#pragma mark - 提现
-(void)tixianAction {
    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
    NSString *iptype = [userDefaul objectForKey:@"iptype"];
    NSString *banben = [userDefaul objectForKey:@"banben"];
        NSString *url = [NSString stringWithFormat:TixiantUrl,[AppData share].username,[Utils md5SignStr],iptype,banben];
    html5MYViewController *html5Vc = [[html5MYViewController alloc] init];
    html5Vc.requestUrl = url;
    html5Vc.titleName = @"提现";
    [self.navigationController pushViewController:html5Vc animated:YES];
    
    
}
//dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {//头部高度
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {//尾部高度
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShopManagerVCCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"ShopManagerVCCell" owner:nil options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 4) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    NSArray *titArr = @[@"批发进货订单",@"批发进货地址",@"我的银行卡",@"我的二维码",@"设置"];
    NSArray *imgArr = @[@"my0.png",@"my6.png",@"my1.png",@"my3.png",@"my5.png"];
    cell.iconImage.image = [UIImage imageNamed:imgArr[indexPath.row]];
    cell.titleLabel.font = [UIFont systemFontOfSize:15];
    cell.titleLabel.text = titArr[indexPath.row];
    cell.lineView.hidden = YES;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {//点击事件
    if (indexPath.section == 0 && indexPath.row == 0) {
        WholeSaleSubVC *orderVC = [[WholeSaleSubVC alloc]init];
        orderVC.loadUrl = [NSString stringWithFormat:OrderManagerM,[AppData share].username];
        orderVC.titleStr = @"订单管理";
        orderVC.isPiFa = YES;
        [self.navigationController pushViewController:orderVC animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 1) {
        WholeSaleSubVC *orderVC = [[WholeSaleSubVC alloc]init];
        orderVC.loadUrl = [NSString stringWithFormat:AddressM,[AppData share].username];
        orderVC.titleStr = @"地址管理";
        orderVC.isPiFa = YES;
        [self.navigationController pushViewController:orderVC animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 2) {
        html5MYViewController *html5MyVC = [[html5MYViewController alloc]init];
        html5MyVC.titleName = @"我的银行卡";
        html5MyVC.showRefresh = @"1";
        NSString *url = [NSString stringWithFormat:myBankCardsURL,[AppData share].username,[Utils md5SignStr]];
        html5MyVC.requestUrl = url;
        [self.navigationController pushViewController:html5MyVC animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 3) {
        //我的二维码
        [self showUserCode];
    }else {
        SetViewController *setview = [[SetViewController alloc] init];
        [self.navigationController pushViewController:setview animated:YES];
    }
}
-(void)showUserCode {
    if(!popView) {
        UIWindow *keyWindow=[UIApplication sharedApplication].keyWindow;
        popView=[[UIView alloc]initWithFrame:keyWindow.bounds];
        popView.backgroundColor=SetColor(0, 0, 0, .4);
        popView.alpha=0;
        [keyWindow addSubview:popView];
        [UIView animateWithDuration:0.35 animations:^{
            popView.alpha=1;
        }];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidentPopView)];
        [popView addGestureRecognizer:tap];
        UIView *showView=[[UIView alloc]initWithFrame:CGRectMake((ScreenWidth-270)/2, (ScreenHeight-270)/2, 270, 270)];
        showView.backgroundColor=[UIColor whiteColor];
        [popView addSubview:showView];
        UILabel *namelable=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, showView.width-20, 35)];
        namelable.text=@"我的二维码";
        namelable.textAlignment = NSTextAlignmentCenter;
        [showView addSubview:namelable];
        barCodeImg=[[UIImageView alloc]initWithFrame:CGRectMake((showView.width-190)/2, 35+(showView.height-70-190)/2, 190, 190)];
        barCodeImg.backgroundColor = MainColor;
        [showView addSubview:barCodeImg];
    }
    NSString *url = [NSString stringWithFormat:BussinessMyCode,[AppData share].username];
    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
    NSString *iptype = [userDefaul objectForKey:@"iptype"];
    //    NSLog(@"-------%@",iptype);
    NSString *tempUrl = [NSString stringWithFormat:@"%@&terminal=%@&OSVersion=IOS1.0.1",url,iptype];
    [barCodeImg sd_setImageWithURL:[NSURL URLWithString:tempUrl] placeholderImage:[UIImage imageNamed:@""]];
}
#pragma mark- 我的二维码

-(void)hidentPopView {
    [UIView animateWithDuration:0.35 animations:^{
        popView.alpha=0;
    } completion:^(BOOL finished) {
        for (UIView *view in popView.subviews) {
            [view removeFromSuperview];
        }
        [popView removeFromSuperview],popView=nil;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
